package com.po;

import com.po.User;
import com.utils.JDBCUnits;

import javax.xml.transform.Result;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class FriendList {
    /**
     * 获取好友的名字
     * @param id
     * @return
     */
    public static List<String> getFriendsName(int id){
        List<String> fname = new ArrayList<>();
        Connection c = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            c = JDBCUnits.getConnectino();
            String sql = "select friend_id from friendList where user_id=?";
            ps = c.prepareStatement(sql);
            ps.setInt(1,id);
            rs = ps.executeQuery();
            while(rs.next()){
                fname.add(getUser(rs.getInt("friend_id")).getName());
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally{
            JDBCUnits.close(c,ps,rs);
        }
        return fname;
    }

    /**
     * 获取所有人的id，当加好友输入id时，有不存在的情况
     * @param id
     * @return
     */
    public static List<Integer> getALl(int id){
        List<Integer> all = new ArrayList<>();
        Connection c = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            c = JDBCUnits.getConnectino();
            String sql = "select id from t_usr where id!=?";
            ps = c.prepareStatement(sql);
            ps.setInt(1,id);
            rs = ps.executeQuery();
            while(rs.next()){
                all.add(rs.getInt("id"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally{
            JDBCUnits.close(c,ps,rs);
        }
        return all;
    }

    /**
     * 获取好友
     * @param id
     * @return
     */
    public static List<User> getFriends(int id){
        List<User> friends = new ArrayList<>();

        Connection c = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            c = JDBCUnits.getConnectino();
            String sql = "select friend_id ,time from friendList where user_id=?";
            ps = c.prepareStatement(sql);
            ps.setInt(1,id);
            rs = ps.executeQuery();
            while(rs.next()){
                User user = getUser(rs.getInt("friend_id"));
                user.setTime(rs.getDate("time"));
                User user1 = getUser(rs.getInt("friend_id"));
                user.setName(user1.getName());
                friends.add(user);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally{
            JDBCUnits.close(c,ps,rs);
        }

        return friends;
    }

    public static User getUser(int id){
        User user = new User();
        Connection c = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            c = JDBCUnits.getConnectino();
            String sql = "select * from t_usr where id = ?";
            ps = c.prepareStatement(sql);
            ps.setInt(1,id);
            rs = ps.executeQuery();
            while(rs.next()){
                user.setSex(rs.getString("sex"));
                user.setName(rs.getString("name"));
                user.setId(rs.getInt("id"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally{
            JDBCUnits.close(c,ps,rs);
        }
        return user;
    }

    public static boolean selectInbox(int user_id,int target_id){
        boolean success = false;
        Connection c = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            c = JDBCUnits.getConnectino();
            String sql = "select * from inbox where user_id=? and target_id=? and way='add'";
            ps = c.prepareStatement(sql);
            ps.setInt(1,user_id);
            ps.setInt(2,target_id);
            rs = ps.executeQuery();
            if(!rs.next()){
                success = true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally{
            JDBCUnits.close(c,ps);
        }
        return success;
    }

    public static int addFriendsInbox(int user_id , int target_id){
        int count = 0;
        Connection c = null;
        PreparedStatement ps = null;

        try {
            c = JDBCUnits.getConnectino();
            String sql = "insert into inbox(way,user_id,target_id) values('add',?,?)";
            ps = c.prepareStatement(sql);
            ps.setInt(1,user_id);
            ps.setInt(2,target_id);
            count = ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally{
            JDBCUnits.close(c,ps);
        }
        return count;
    }

    public static User getInformation(String user_name){
        User user = new User();
        Connection c = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            c = JDBCUnits.getConnectino();
            String sql = "select * from t_usr where name = ?";
            ps = c.prepareStatement(sql);
            ps.setString(1,user_name);
            rs = ps.executeQuery();
            if(rs.next()){
                user.setName(rs.getString("name"));
                user.setId(rs.getInt("id"));
                user.setSex(rs.getString("sex"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally{
            JDBCUnits.close(c,ps,rs);
        }

        return user;
    }

    public static void sendMessage(int id,int user_id,String content){
        Connection c = null;
        PreparedStatement ps = null;

        try {
            c = JDBCUnits.getConnectino();
            String sql = "insert into charwithfriend(id,friend_id,chatContent,chatTime) values(?,?,?,now())";
            ps = c.prepareStatement(sql);
            ps.setInt(1,id);
            ps.setInt(2,user_id);
            ps.setString(3,content);
            ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally{
            JDBCUnits.close(c,ps);
        }
    }
}
